package com.ysd.lis.controller.inv;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ysd.common.Result;
import com.ysd.lis.controller.pub.BaseController;
import com.ysd.lis.entity.inv.InvOpenemptyboxLoad;
import com.ysd.lis.entity.inv.InvReagent;
import com.ysd.lis.request.inv.InvOpenEmptyBoxParam;
import com.ysd.lis.service.inv.InvOpenemptyboxLoadService;
import com.ysd.lis.service.inv.InvReagentService;
import io.lettuce.core.output.ScoredValueOutput;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 订单信息 前端控制器
 * </p>
 *
 * @author WH
 * @since 2024-09-19
 */
@RestController
@RequestMapping("/inv/openemptybox")
public class InvOpenEmptyBoxController extends BaseController {

    @Autowired
    InvOpenemptyboxLoadService invOpenemptyboxLoadService;
    @Autowired
    InvReagentService invReagentService;


    @ApiOperation("查询")
    @GetMapping("/findOpenEmptyBoxList")
    public Result findOpenEmptyBoxList(InvOpenEmptyBoxParam param) {
        LambdaQueryWrapper<InvOpenemptyboxLoad> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvOpenemptyboxLoad::getDelFlag, 0);
        if (StrUtil.isNotEmpty(param.getInstrCode())) {
            lambdaQueryWrapper.eq(InvOpenemptyboxLoad::getInstrCode, param.getInstrCode());
        }
        if (StrUtil.isNotEmpty(param.getBarcode())) {
            lambdaQueryWrapper.eq(InvOpenemptyboxLoad::getBarcode, param.getBarcode());
        }
        if (StrUtil.isNotEmpty(param.getType())) {
            lambdaQueryWrapper.eq(InvOpenemptyboxLoad::getStatus, param.getType());
        }
        List<InvOpenemptyboxLoad> list = invOpenemptyboxLoadService.list(lambdaQueryWrapper);
        return Result.succ(1, "查询成功", list);
    }

    @ApiOperation("新增")
    @PostMapping("/addOpenEmptyBox")
    public Result addOrderInfo(@RequestBody InvOpenemptyboxLoad invOpenemptyboxLoad) {
        //根据barcode查询一下是否存在入库信息
        LambdaQueryWrapper<InvOpenemptyboxLoad> openQueryWrapper = new LambdaQueryWrapper<>();
        openQueryWrapper.eq(InvOpenemptyboxLoad::getBarcode, invOpenemptyboxLoad.getBarcode())
                .eq(InvOpenemptyboxLoad::getStatus, invOpenemptyboxLoad.getStatus())
                .eq(InvOpenemptyboxLoad::getDelFlag, 0);
        int count = invOpenemptyboxLoadService.count(openQueryWrapper);
        if (count > 0) {
            return Result.fail("该条码的试剂已存在请勿重复扫码");
        }
        //空盒登记需要有开盒登记信息
        if ("2".equals(invOpenemptyboxLoad.getStatus())) {
            LambdaQueryWrapper<InvOpenemptyboxLoad> openQueryWrapper2 = new LambdaQueryWrapper<>();
            openQueryWrapper2.eq(InvOpenemptyboxLoad::getBarcode, invOpenemptyboxLoad.getBarcode())
                    .eq(InvOpenemptyboxLoad::getStatus, "1")
                    .eq(InvOpenemptyboxLoad::getDelFlag, 0);
            int count2 = invOpenemptyboxLoadService.count(openQueryWrapper);
            if (count2 == 0) {
                return Result.fail("该条码不存在开盒信息，无法空盒操作");
            }
        }

        //根据barcode入库
        LambdaQueryWrapper<InvReagent> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(InvReagent::getBarcode, invOpenemptyboxLoad.getBarcode())
                .eq(InvReagent::getDelFlag, 0);
        List<InvReagent> list = invReagentService.list(lambdaQueryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            for (InvReagent invReagent : list) {
                invOpenemptyboxLoad.setReagentCode(invReagent.getReaNo());
                invOpenemptyboxLoadService.save(invOpenemptyboxLoad);
            }
            return Result.succ(1, "新增成功", invOpenemptyboxLoad);
        } else {
            return Result.fail("根据条码未查到试剂信息");
        }
    }

    @ApiOperation("编辑")
    @PostMapping("/editOpenEmptyBox")
    public Result editOrderInfo(@RequestBody InvOpenemptyboxLoad invOpenemptyboxLoad) {
        boolean edit = invOpenemptyboxLoadService.updateById(invOpenemptyboxLoad);
        if (edit) {
            return Result.succ(1, "新增成功", invOpenemptyboxLoad);
        }
        return Result.fail("新增失败");
    }

}
